package edu.vt.ece3574.ninjatag;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.android.FriendsGetProfilePics;
import com.facebook.android.Utility;

public class AppInviteListAdapter extends BaseAdapter {
	private final Activity mInstance;
	private LayoutInflater mInflater;
	private JSONArray jsonArray;
	private SparseBooleanArray out;

	public AppInviteListAdapter(Activity friendsList, JSONArray jsonArray) {
		synchronized (jsonArray) {
			this.jsonArray = jsonArray;
		}
		mInstance = friendsList;
		if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        Utility.model.setListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());
    }

    @Override
    public int getCount() {
    	/*
    	int i=0;
    	synchronized(jsonArray) {
	    	try {
	    		while(true) {
	    			jsonArray.get(i);
	    			i++;
	    		}
	    	} catch (Exception e) {
	    		//As soon as I figure out a better way to do this, I'll do it that way.
	    		//This is worse than a hack, it's an exploit.
	    	}
    	return i;
        /*/
    	synchronized (jsonArray) {
    		return jsonArray.length();//*/
		}
    }

    @Override
    public Object getItem(int position) {
        JSONObject jsonObject = null;
        synchronized (jsonArray) {
			try {
				jsonObject = jsonArray.getJSONObject(position);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return jsonObject;
    }

    @Override
    public long getItemId(int position) {
        try {
			return Long.valueOf(((JSONObject) getItem(position)).getString("uid"));
		} catch (NumberFormatException e) {
			Log.e("NumberFormatException",e.getMessage());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.e("JSONException",e.getMessage());
			e.printStackTrace();
		}
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        JSONObject jsonObject = null;
        synchronized (jsonArray) {
			try {
				jsonObject = jsonArray.getJSONObject(position);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		View hView = convertView;
        if (convertView == null) {
            hView = mInflater.inflate(R.layout.friend_item, null, false);
            hView.setClickable(true);
            
            ViewHolder holder = new ViewHolder();
            holder.profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
            holder.name = (TextView) hView.findViewById(R.id.name);
            holder.info = (TextView) hView.findViewById(R.id.info);
            holder.checkBox = (CheckBox) hView.findViewById(R.id.checkBox1);
            synchronized (jsonArray) {
				if (out == null || out.size() != jsonArray.length()) {
					int i=getCount();
					if (i != 0) {
						out = new SparseBooleanArray(getCount());
					}
				}
			}
			holder.checkBox.setChecked(out.valueAt(position));
            holder.position=position;
            hView.setTag(holder);
        }

        final ViewHolder holder = (ViewHolder) hView.getTag();
        try {
            holder.profile_pic.setImageBitmap(Utility.model.getImage(
                    jsonObject.getString("uid"), jsonObject.getString("pic_square")));
        } catch (JSONException e) {
            holder.name.setText("");
        }
        try {
            holder.name.setText(jsonObject.getString("name"));
        } catch (JSONException e) {
            holder.name.setText("");
        }
        try {
            JSONObject location = jsonObject.getJSONObject("current_location");
            holder.info.setText(location.getString("city") + ", "
                    + location.getString("state"));
        } catch (JSONException e) {
            holder.info.setText("");
        }

        //holder.checkBox.setOnClickListener(holder.checkBox); //This was part of an example, I'm not sure if it's necessary
        hView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
				int i = getCount(); // For debugging.
            	synchronized (jsonArray) {
					assert (i == jsonArray.length());
					holder.checkBox.toggle();
					if (out == null || out.size() != jsonArray.length()) {
						int j=getCount();
						if (j != 0) {
							out = new SparseBooleanArray(getCount());
						}
					}
				}
				out.append(holder.position, holder.checkBox.isChecked());
        	}
        });
        
        return hView;
    }

    class ViewHolder {
        ImageView profile_pic;
        TextView name;
        TextView info;
        CheckBox checkBox;
        int position;
    }

    SparseBooleanArray getCheckedItemPositions(ListView friendsList) {
    	synchronized (jsonArray) {
			if (out == null || out.size() != jsonArray.length()) {
				int i=getCount();
				if (i != 0) {
					out = new SparseBooleanArray(getCount());
				}
			}
		}
		return out;
	}

}